﻿using System;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;

namespace BugClient
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            inifile = AppDomain.CurrentDomain.BaseDirectory + "config.ini";
            serverAddressTextBox.Text = INIOperationClass.INIGetStringValue(inifile, section, "ServerAddress", "http://");
            userNameTextBox.Text = INIOperationClass.INIGetStringValue(inifile, section, "Account", "");
            passwordTextBox.Text = INIOperationClass.INIGetStringValue(inifile, section, "Password", "");
            remPasswordCheck.Checked = INIOperationClass.INIGetStringValue(inifile, section, "RemPassword", "1") == "1";
            autoLoginCheck.Checked = INIOperationClass.INIGetStringValue(inifile, section, "AutoLogin", "0") == "1";
            init = true;
        }
        const string section = "General";
        string inifile;
        bool init = false;
        public string url;
        public WebClient web = new WebClient();
        public JsMain maininfo;

        private async void loginButton_Click(object sender, EventArgs e)
        {
            if (web.IsBusy)
                return;
            url = serverAddressTextBox.Text;
            string response;
            try
            {
                response = await web.UploadStringTaskAsync(url + "/login", string.Format("id={0}&password={1}", HttpUtility.UrlEncode(userNameTextBox.Text), HttpUtility.UrlEncode(passwordTextBox.Text)));
            }
            catch (WebException ex)
            {
                MessageBox.Show("连接服务器失败：" + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接服务器失败：" + ex.Message);
                return;
            }
            if (string.IsNullOrEmpty(response))
            {
                MessageBox.Show("登录失败");
                return;
            }
            try
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                maininfo = js.Deserialize<JsMain>(response);
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录失败：" + ex.Message);
                return;
            }

            INIOperationClass.INIWriteValue(inifile, section, "ServerAddress", url);
            INIOperationClass.INIWriteValue(inifile, section, "Account", userNameTextBox.Text);
            if (remPasswordCheck.Checked)
                INIOperationClass.INIWriteValue(inifile, section, "Password", passwordTextBox.Text);

            DialogResult = DialogResult.OK;
            Dispose();
            Close();
        }

        private void remPasswordCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (init)
            {
                INIOperationClass.INIWriteValue(inifile, section, "RemPassword", remPasswordCheck.Checked ? "1" : "0");
                if (!remPasswordCheck.Checked && autoLoginCheck.Checked)
                    autoLoginCheck.Checked = false;
                if(!remPasswordCheck.Checked)
                    INIOperationClass.INIWriteValue(inifile, section, "Password", "");
            }
        }

        private void autoLoginCheck_CheckedChanged(object sender, EventArgs e)
        {
            if (init)
            {
                INIOperationClass.INIWriteValue(inifile, section, "AutoLogin", autoLoginCheck.Checked ? "1" : "0");
                if (!remPasswordCheck.Checked && autoLoginCheck.Checked)
                    remPasswordCheck.Checked = true;
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            if (autoLoginCheck.Checked && passwordTextBox.Text.Length > 0)
                loginButton_Click(null, null);
        }
    }
}
